﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using System.Drawing;
using NPOI.SS.UserModel;
using IHISCETeim.Model;
using System.Reflection;
using System.IO;
using NPOI.SS.Util;


namespace IHISCETeim.Web.Export.AgentSys
{
    public partial class AgentPWPartList : ExportPageBase
    {
        protected override string xlsTemplateFileName
        {
            get
            {
                return "/Agent/保外配件销售清单.xls";
            }
        }
        protected override string SheetName
        {
            get
            {
                return "sheet1";
            }
        }

        [QueryColumnName(), QueryOperator(QueryOperatorsEnum.包含)]
        public string AgentName
        {
            get
            {
                return GetRequestValue<string>("AgentName", "");

            }
        }
        [QueryColumnName(), QueryOperator(QueryOperatorsEnum.包含)]
        public string OrderNumber
        {
            get
            {
                return GetRequestValue<string>("OrderNumber", "");

            }
        }
        [QueryColumnName(), QueryOperator(QueryOperatorsEnum.包含)]
        public string SerialNumber
        {
            get
            {
                return GetRequestValue<string>("SerialNumber", "");

            }
        }
        [QueryColumnName(), QueryOperator(QueryOperatorsEnum.包含)]
        public string CustomerName
        {
            get
            {
                return GetRequestValue<string>("CustomerName", "");

            }
        }
        public string IssueDateForm
        {
            get
            {
                return GetRequestValue<string>("IssueDateForm", "");

            }
        }

        public string IssueDateTo
        {
            get
            {
                if (string.IsNullOrEmpty(IssueDateForm))
                {
                    return "";
                }
                else
                {
                    string todate = GetRequestValue<string>("IssueDateTo", "");
                    if (string.IsNullOrEmpty(todate))
                        return Convert.ToDateTime(IssueDateForm).AddDays(1).ToShortDateString();
                    else
                        return todate;
                }

                // return GetRequestValue<string>("IssueDateTo", "");

            }
        }
        protected string GetSql()
        {
            string sqlwhere = QueryFiledHelper.GetSqlWhere(this);

            if (!string.IsNullOrEmpty(IssueDateForm))
            {
                string DOWNSQL = QueryOperatorsEnumHelper.ConvertCommonOperator("IssueDate", QueryOperatorsEnum.范围, IssueDateForm + "|" + IssueDateTo);
                if (!string.IsNullOrEmpty(sqlwhere))
                {
                    sqlwhere += " and " + DOWNSQL;
                }
                else
                {
                    sqlwhere = DOWNSQL;
                }
            }
            //string _agentname = base.User.IsSuperAdmin ? "" : HasPower(Permission.经销商平台_经销商) ? OUserController.MutlCmpStr(base.User.UserId) : "";
            //if (!string.IsNullOrEmpty(_agentname))
            //{
            //    string sqlcommonFiled = string.Format("AgentName in {0}", _agentname);
            //    if (!string.IsNullOrEmpty(sqlwhere))
            //    {
            //        sqlwhere += " and " + sqlcommonFiled;
            //    }
            //    else
            //    {
            //        sqlwhere = sqlcommonFiled;
            //    }
            //}

            return sqlwhere;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            InitializeWorkbook();
            ISheet sheet1 = hssfworkbook.GetSheet(SheetName);
            //所有需要导出的ID
            int TotalCount = 0;
            DataTable dtList = AgentPartOrderController.GetPartListReport(GetSql());
            int rowindexBase = 2;
            int rowindex = 0;
            int rowReal = rowindex + rowindexBase;


            foreach (DataRow listDTDR in dtList.Rows)
            {
                int cellReal = 0;
                HSSFRow writerow = (HSSFRow)sheet1.CreateRow(rowReal);
                writerow.CreateCell(cellReal++).SetCellValue(rowReal-1);

                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["OrderNumber"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["AgentName"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["ItemCode"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(listDTDR["Dscription"].ToString());
                writerow.CreateCell(cellReal++).SetCellValue(Convert.ToDouble(listDTDR["Quantity"]));
                writerow.CreateCell(cellReal++).SetCellValue(Convert.ToDouble(listDTDR["OldPrice"]));
                writerow.CreateCell(cellReal++).SetCellValue(GetBuyType(listDTDR["BuyType"].ToString()));
                writerow.CreateCell(cellReal++).SetCellValue(Convert.ToDouble(listDTDR["Price"]));
                writerow.CreateCell(cellReal++).SetCellValue(Convert.ToDouble(listDTDR["LineTotal"]));
                rowReal++;
            }

            //writerowSum.CreateCell(2).SetCellFormula(string.Format("sum(C3:C{0})", rowReal));          

            WriteToFile();
        }

        private string GetBuyType(string oldBuyType)
        {
            switch (oldBuyType)
            {
                case "库存订货":
                    return "库存订货(8.5折)";
                    break;
                case "紧急订货":
                    return "紧急订货(9折)";
                    break;
            }
            return oldBuyType;
        }

        protected override string GetXlsFileName(string oldFileName)
        {
            //return base.GetXlsFileName(oldFileName);

            return string.Format("{0}{1}.xls", "保外服务支持配件销售明细", DateTime.Now.ToString("yyyy-MM-dd")).Trim();
        }
    }
}